package com.jike.noobmoney.mvp.view.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MoneyEntity;
import com.jike.noobmoney.helper.ImageLoader;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.mvp.view.activity.ChongZhiActivity;
import com.jike.noobmoney.mvp.view.activity.LianXiUsActivity;
import com.jike.noobmoney.mvp.view.activity.MainActivity;
import com.jike.noobmoney.mvp.view.activity.MyInfoActivity;
import com.jike.noobmoney.mvp.view.activity.MyPushActivity;
import com.jike.noobmoney.mvp.view.activity.MyTaskActivity;
import com.jike.noobmoney.mvp.view.activity.RankActivity;
import com.jike.noobmoney.mvp.view.activity.RankMouthActivity;
import com.jike.noobmoney.mvp.view.activity.SettingActivity;
import com.jike.noobmoney.mvp.view.activity.ShouZhiActivity;
import com.jike.noobmoney.mvp.view.activity.TiXianActivity;
import com.jike.noobmoney.mvp.view.activity.TiXianCenterActivity;
import com.jike.noobmoney.mvp.view.activity.VideoTwoActivity;
import com.jike.noobmoney.mvp.view.widget.CommonDialog;
import com.jike.noobmoney.mvp.view.widget.TiXianDialog;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.rxbus.RxBus;
import com.jike.noobmoney.rxbus.RxBusRoute;
import com.jike.noobmoney.rxbus.RxEvent;
import com.jike.noobmoney.util.SPUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by kekex on 2018/8/21.
 * Home
 */
public class MyFragment extends BaseFragment implements IView {

    @BindView(R.id.tv_center_title)
    TextView tvTitle;
    @BindView(R.id.iv_user_photo)
    ImageView ivUserPhoto;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_id)
    TextView tvUserId;

    @BindView(R.id.tv_money)
    TextView tvMoney;

    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.rl_my_task)
    RelativeLayout rlMyTask;
    @BindView(R.id.rl_question)
    RelativeLayout rlQuestion;
    @BindView(R.id.rl_cooperation)
    RelativeLayout rlCooperation;
    @BindView(R.id.rl_about_us)
    RelativeLayout rlAboutUs;
    @BindView(R.id.tv_login_out)
    TextView tvLoginOut;
    private UserPresenter userPresenter;

    @Override
    protected int layoutResID() {
        return R.layout.fragment_my;
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshData();
    }


    private void refreshData() {
        if (userPresenter != null) {
            showProgressDialog();
            String userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
            userPresenter.moneyView(userid, ConstantValue.RequestKey.getMoneyView);
        }
    }


    @Override
    protected void initData() {
        tvTitle.setText("个人中心");
        userPresenter = new UserPresenter(this);
        //修改个人信息更新
        initView();

    }

    @OnClick({R.id.ll_user, R.id.tv_withdraw, R.id.iv_my_invite, R.id.iv_my_newteach,
            R.id.rl_rank_list, R.id.rl_new_shouzhi, R.id.rl_chongzhi, R.id.rl_tixian, R.id.rl_my_task, R.id.rl_my_push,
            R.id.rl_lianxi_us, R.id.rl_setting, R.id.tv_login_out,
            R.id.tv_new_chongzhi, R.id.tv_new_tixian
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.dtv_szmx:
//                //收支明细
//                //startActivityForResult(new Intent(context, ShouZhiActivity.class), 100);
//                ShouZhiActivity.startAction(context, 0);
//                break;
            case R.id.ll_user:
//            case R.id.dtv_wdzl:
                //我的资料
                startActivity(new Intent(context, MyInfoActivity.class));
                break;
            case R.id.tv_withdraw:
                //立即提现
                //tixian();
                break;
            case R.id.iv_my_invite:
                MainActivity activity1 = (MainActivity) getActivity();
                if (activity1 != null) {
                    activity1.goRecommend(3);
                }
                break;
            case R.id.iv_my_newteach:
                //新手教程
                Intent intent = new Intent(context, VideoTwoActivity.class);
                intent.putExtra("isTask", false);
                startActivity(intent);
                break;
            case R.id.rl_rank_list:
                //排行榜
                startActivity(new Intent(context, RankActivity.class));
                break;
            case R.id.rl_new_shouzhi:
                //收支明细
                ShouZhiActivity.startAction(context, 0);
                break;
            case R.id.rl_chongzhi:
            case R.id.tv_new_chongzhi:
                //充值中心
                startActivity(new Intent(context, ChongZhiActivity.class));
                break;
            case R.id.rl_tixian:
            case R.id.tv_new_tixian:
                //提现中心ok
                startActivity(new Intent(context, TiXianCenterActivity.class));
                break;
            case R.id.rl_my_task:
                //我的任务
                startActivity(new Intent(context, MyTaskActivity.class));
                break;
            case R.id.rl_my_push:
                //我的发布
                startActivity(new Intent(context, MyPushActivity.class));
                break;
            case R.id.rl_lianxi_us:
                //联系我们
                startActivity(new Intent(context, LianXiUsActivity.class));
                break;
            case R.id.rl_setting:
                //设置
                startActivity(new Intent(context, SettingActivity.class));
                break;
            case R.id.tv_login_out:
                //退出登录
                loginOut();
                break;
        }
    }

    /**
     * 提现
     */
    private void tixian() {
        TiXianDialog bottomDialog = TiXianDialog.getInstance();
        bottomDialog.show(getFragmentManager(), "tixian");
        bottomDialog.setClickCallback(() -> {
            Intent intent = new Intent(context, TiXianActivity.class);
            startActivityForResult(intent, 100);
            bottomDialog.dismiss();
        });
    }

    /**
     * 退出登录
     */
    private void loginOut() {
        CommonDialog loginOutDialog = CommonDialog.getInstance();
        loginOutDialog.show(getFragmentManager(), "loginout");
        loginOutDialog.setClickCallback(new CommonDialog.OnClickCallback() {
            @Override
            public void cancel() {
                loginOutDialog.dismiss();
            }

            @Override
            public void confirm() {
                RxBus.getInstance().post(new RxEvent(RxBusRoute.LOGIN_OUT));
                loginOutDialog.dismiss();
            }
        });
    }

    @Override
    protected boolean isRegistRxBus() {
        return true;
    }

    /**
     * 注册订阅事件
     */
    protected void receiveEvent(RxEvent rxEvent) {
        switch (rxEvent.busName) {
            case RxBusRoute.REFRESH_USER_INFO:
                initView();
                break;
        }
    }

    private void initView() {
        String nick = SPUtils.getInstance().getString(ConstantValue.SpType.nick);
        String userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        //String avatar = SPUtils.getInstance().getString(ConstantValue.SpType.avatar);
        String newavatar = SPUtils.getInstance().getString(ConstantValue.SpType.newavatar);

        if (TextUtils.isEmpty(newavatar)) {
            if (ivUserPhoto != null) {
                ImageLoader.displayImageDefaultHead(context, R.drawable.ic_user_default, ivUserPhoto);
            }
        } else {
            if (ivUserPhoto != null) {
                String url = ConstantValue.IMG_BASEURL + newavatar;
                ImageLoader.displayImageDefaultHead(context, url, ivUserPhoto);
            }
        }

        //昵称
        if (tvUserName != null) {
            tvUserName.setText(TextUtils.isEmpty(nick) ? "点击设置昵称" : nick);
        }

        //用户id
        if (tvUserId != null) {
            tvUserId.setText("ID:" + userid);
        }


    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgressDialog();
        if (ConstantValue.RequestKey.getMoneyView.equals(actionType)) {
            //获取用户余额
            MoneyEntity.UserBean userBean = (MoneyEntity.UserBean) data;
            tvMoney.setText("" + userBean.getMoney());
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        dismissProgressDialog();
    }
}
